package com.flex.mobile.util;

import com.flex.mobile.dao.impl.UserStatusDao;
import com.flex.mobile.dao.model.UserStatus;
import com.flex.mobile.model.User;

import flex.messaging.FlexSession;
import flex.messaging.MessageClient;
import flex.messaging.MessageClientListener;

public class MyMessageClientListener implements MessageClientListener {

   @Override
   public void messageClientCreated(MessageClient messageClient) {
      System.out.println("MessageClient created: " + messageClient.getClientId());
      messageClient.addMessageClientDestroyedListener(this); 

   }

   @Override
   public void messageClientDestroyed(MessageClient messageClient) {
      FlexSession session = messageClient.getFlexSession();
      User user = (User)session.getAttribute("user");
      UserStatusDao userStatusDao = (UserStatusDao)SpringContextUtil.getBean("userStatusDao");
      userStatusDao.updateStatusByTid(UserStatus.CURRENT_STATUS_OFFLINE, user.getTid());
      System.out.println("MessageClient destroyed: " + messageClient.getClientId()); 
   }

}
